<template>
	<view class="user-layout" :style="{'background-image': 'url('+ bgUrl +')'}">
		<customNav :opacity="0" />
		<view style="margin-top: 30px;">
			<view class="user-title">{{isLoginStatus ? '密码登录':'注册'}} </view>
			<view class="user-subtitle">
				<view class="size16">{{isLoginStatus ? '没有账号？':'已有账号？'}}</view>
				<view v-if="isLoginStatus" class="nav size16" @click="toRegister">
					立即注册
				</view>
				<view v-else class="nav size16" @click="toLogin">
					立即登录
				</view>
			</view>
			<!-- 登录 -->
			<view v-if="isLoginStatus">
				<view class="user-form">
					<uni-forms ref="formRef" :modelValue="formData" :rules="formRules">
						<uni-forms-item label="手机号" name="phone">
							<uni-easyinput type="text" v-model="formData.phone" placeholder="请输入手机号"
								:placeholderStyle="placeholderStyle" />
						</uni-forms-item>
						<uni-forms-item label="密码" name="pass_word">
							<uni-easyinput type="password" v-model="formData.pass_word" placeholder="请输入密码"
								:placeholderStyle="placeholderStyle" />
						</uni-forms-item>
					</uni-forms>
				</view>
				<view style="text-align: end;margin-right: 10px;">
					<view style="color: #007aff;font-size: 12px;padding: 0 10px;display: inline-block;" @click="goForgetPass">忘记密码？</view>
				</view>
				<view class="user-btn">
					<u-button type="primary" text="登录" @click="formSubmit"
						customStyle="width: 100%;height:46px;font-size: 14px;"></u-button>
					<view class="margin-t15" style="display: flex;align-items: center;">
						<img v-if="!checked" src="@/static/images/no_select.png"
							style="width:20px;height: 20px;margin-right: 10px;" alt="" @click="checked = true" />
						<img v-else src="@/static/images/select.png" alt=""
							style="width:20px;height: 20px;margin-right: 10px;" @click="checked = false" />
						<text>我已阅读并同意</text>
						<text style="color: #007aff;" @click="goXieYi">"用户服务协议"</text>
					</view>
				</view>
			</view>
			<!-- 注册 -->
			<view v-else>
				<view class="user-form">
					<uni-forms ref="formRegisterRef" :modelValue="formRegisterData" :rules="formRegisterRules">
						<uni-forms-item label="姓名" name="appName">
							<uni-easyinput type="text" v-model="formRegisterData.appName" placeholder="请输入姓名"
								:placeholderStyle="placeholderStyle" />
						</uni-forms-item>
						<uni-forms-item label="手机号" name="appPhone">
							<uni-easyinput type="text" v-model="formRegisterData.appPhone" placeholder="请输入手机号"
								:placeholderStyle="placeholderStyle" />
						</uni-forms-item>
						<uni-forms-item label="密码" name="appPassword">
							<uni-easyinput type="password" v-model="formRegisterData.appPassword"
								placeholder="请输入6-16位密码" :placeholderStyle="placeholderStyle" />
						</uni-forms-item>
						<uni-forms-item label="确认密码" name="qrpassword">
							<uni-easyinput type="password" v-model="formRegisterData.qrpassword" placeholder="请再次输入密码"
								:placeholderStyle="placeholderStyle" />
						</uni-forms-item>
					</uni-forms>
				</view>
				<view class="user-btn">
					<u-button type="primary" text="注册" @click="formRegisterSubmit"
						customStyle="width: 100%;height:46px;font-size: 14px;"></u-button>
					<view class="margin-t15" style="display: flex;align-items: center;">
						<img v-if="!checked" src="@/static/images/no_select.png"
							style="width:20px;height: 20px;margin-right: 10px;" alt="" @click="checked = true" />
						<img v-else src="@/static/images/select.png" alt=""
							style="width:20px;height: 20px;margin-right: 10px;" @click="checked = false" />
						<text>我已阅读并同意</text>
						<text style="color: #007aff;" @click="goXieYi">"用户服务协议"</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import env from "@/config/env.js";
	import customNav from "@/components/nav/index.vue"
	import {
		userLogin,
		userRegister
	} from '@/api/user/index.js';
	export default {
		components: {
			customNav
		},
		data() {
			return {
				bgUrl: env.baseUrl + "/file/get_file?file_name=homebg.png",
				placeholderStyle: 'font-size:16px',
				formData: {
					phone: '',
					pass_word: '',
				},
				formRules: {
					phone: {
						rules: [{
							required: true,
							errorMessage: '请输入账号',
						}, {
							pattern: "^1[3-9]\\d{9}$",
							errorMessage: "请输入有效的手机号"
						}],
					},
					pass_word: {
						rules: [{
							required: true,
							errorMessage: '请输入密码',
						}],
					},
				},

				formRegisterRef: null,
				formRegisterData: {
					appPhone: "",
					appName: "",
					appPassword: "",
					qrpassword: "",
				},
				formRegisterRules: {
					appName: {
						rules: [{
							required: true,
							errorMessage: '请输入姓名',
						}]
					},
					appPhone: { // 确保字段名与表单model一致
						rules: [{
							required: true,
							errorMessage: "请输入手机号",
						}, {
							pattern: "^1[3-9]\\d{9}$",
							errorMessage: "请输入有效的手机号"
						}]
					},
					appPassword: {
						rules: [{
							required: true,
							errorMessage: "请输入6-16位密码",
						}, {
							minLength: 6, 
							maxLength: 16,
							errorMessage: "请输入6-16位密码"
						}]
					},
					qrpassword: {
						rules: [{
							required: true,
							errorMessage: "请确认密码",
						}]
					}
				},
				loading: false, // 登录按钮的加载状态

				isLoginStatus: true, //是注册还是登录状态
				checked: false,
			};
		},
		methods: {
			goXieYi() {
				uni.navigateTo({
					url: "/pages/login/userPryvacyProtocol"
				});
			},
			toRegister() {
				this.isLoginStatus = false;
				this.$nextTick(() => {
					if (this.$refs.formRegisterRef) {
						this.$refs.formRegisterRef.clearValidate();
					}
				})
			},
			toLogin() {
				this.isLoginStatus = true;
				this.$nextTick(() => {
					console.log(this.$refs.formRef)
					if (this.$refs.formRef) {
						this.$refs.formRef.clearValidate();
					}
				})
			},

			// 登录
			async formSubmit() {
				// 如果正在加载中，直接返回
				if (this.loading) return;
				// 表单验证
				const valid = await this.$refs.formRef.validate();
				if (valid) {
					if (this.checked == false) {
						uni.showToast({
							title: "请勾选用户服务协议",
							icon: "none"
						});
						return
					}
					this.loading = true; // 开始加载
					// 调用登录接口
					let params = {
						phone: this.formData.phone,
						pass_word: Number(this.formData.pass_word),
					}
					const res = await userLogin(params);
					this.loading = false;
					if (res.status == 200) {
						uni.setStorageSync('is_login', true);
						uni.setStorageSync('user_info', res.data);
						uni.reLaunch({
							url: '/pages/index/home/index',
						});
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none"
						});
					}
				} else {
					this.loading = false;
				}
			},

			//注册
			async formRegisterSubmit() {
				// 如果正在加载中，直接返回
				if (this.loading) return;
				// 表单验证
				const valid = await this.$refs.formRegisterRef.validate();
				if (valid) {
					if (this.checked == false) {
						uni.showToast({
							title: "请勾选用户服务协议",
							icon: "none"
						});
						return
					}
					this.loading = true; // 开始加载
					// 调用登录接口
					let params = {
						appPhone: this.formRegisterData.appPhone,
						appName: this.formRegisterData.appName,
						appPassword: this.formRegisterData.appPassword,
					}
					if (this.formRegisterData.appPassword != this.formRegisterData.qrpassword) {
						this.loading = false;
						uni.showToast({
							title: "两次输入的密码不一致",
							icon: "none"
						});
						return false;
					}
					const res = await userRegister(params);
					this.loading = false;
					if (res.status == 200) {
						uni.setStorageSync('is_login', true);
						uni.setStorageSync('user_info', res.data);
						uni.reLaunch({
							url: '/pages/index/home/index',
						});
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none"
						});
					}
				} else {
					this.loading = false
				}
			},
			goForgetPass() {
				uni.navigateTo({
					url: "/pages/login/forgetPass"
				});
			}
		},
	};
</script>

<style scoped>
	/* 样式可以根据需要自行调整 */
	.user-layout {
		padding: 0px px 0 0px;
		box-sizing: border-box;
		height: 100vh;
		background-size: contain;
		background-repeat: no-repeat;
		background-color: #F8F9F9;
	}

	.user-title {
		font-size: 24px;
		font-weight: bold;
		margin-bottom: 10px;
	}

	.user-subtitle {
		display: flex;
		align-items: center;
		margin-bottom: 10px;
	}

	.nav {
		color: #007aff;
		font-weight: bold;
	}

	.user-form {
		margin-bottom: 20px;
	}

	.user-btn .btn {
		width: 100%;
	}
</style>